Refresh relay auth tokens before they expire - #172
Merged
Conversation
steventux
force-pushed
the
DTOSS-13334-auto-renew-relay-token
branch
3 times, most recently
from
July 23, 2026 07:00
99df33b to
a5ac501
Compare
steventux
marked this pull request as ready for review
July 23, 2026 07:02
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the Azure Relay listener to proactively reconnect based on token expiry time, aiming to prevent daily production disconnects caused by Managed Identity token expiry.
Changes:
- Refactors
RelayListener.listen()into a reconnect loop that refreshes the connection when a timeout occurs. - Introduces
RelayURI.connection_details()to return both the connection URL and token expiry time (bearer/SAS). - Updates unit/integration test scaffolding to accommodate the new listen/connection flow and logging format.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/relay_listener.py | Adds token-expiry-aware connection lifecycle and URL/expiry retrieval via connection_details() |
| tests/test_relay_listener.py | Refactors tests to target _listen_on_connection() and verifies reconnect behavior and logging |
| tests/conftest.py | Updates fake_relay harness to align with the new listener behavior in integration-style tests |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The relay connection token expires every 24 hours for MI tokens and has a predefined expiry for SAS tokens. By monitoring the time the connection has been open in an outer loop we can force the refresh of the token before it expires.
steventux
force-pushed
the
DTOSS-13334-auto-renew-relay-token
branch
from
July 23, 2026 07:13
a5ac501 to
2aeea01
Compare
| async with self._connect(connection_url) as websocket: | ||
| logger.info("Connected - waiting for worklist actions...") | ||
| await self._listen_on_connection(websocket, refresh_at) | ||
| except asyncio.TimeoutError: |
Contributor
There was a problem hiding this comment.
I think this also catches an open-handshake timeout from self._connect() on line 86. Suggest a custom signal eg TokenRefreshDue(Exception) raised at line 97 and caught here. Relay connection failures then fall through to main()'s existing backoff.
carlosmartinez
previously approved these changes
Jul 23, 2026
Distinguish between TimeoutError and a token expired event by raising a custom error.
carlosmartinez
approved these changes
Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Monitor the connection age of the Azure Relay Listener and refresh the credential token on the connection url before it times out.
This prevents daily disconnects in production, the Managed Identity token expiry is 24 hours.
Jira link
https://nhsd-jira.digital.nhs.uk/browse/DTOSS-13334
Review notes
Review checklist